﻿unit PmItemClassService;

interface
{$I ../Config.inc}
uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm,
  PmItemClassModel,
  PmItemClassBLL,
  PmItemDataModel;
  
type


  TPmItemClassService = class(TPmItemClassBLL)
  private

  protected

  public
  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TPmItemClass; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TPmItemClassList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TPmItemClass; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TPmItemClassList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function Check(const iItem:TPmItemClass; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TPmItemClassList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeExecute(const iItem:TPmItemClass; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TPmItemClassList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function AfterTreatment(const iItem:TPmItemClass; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TPmItemClassList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function CalculateIdAndSortNo(const iPid:string; out oNewSortNo:Integer; out oNewId,oError:string):Boolean;
  {$ENDIF}
    function LoadAll(const iItems:TPmItemClassList; out oError:string; const iJustUse:Boolean=False):Boolean;
    function LoadById(const iItemClassId:string; const iItem:TPmItemClass; out oError:string):Boolean;
    function ExchangeSortNo(const iItem1:TPmItemClass; const iItem2:TPmItemClass; out oError:string):Boolean;
    function Use(const iItem:TPmItemClass; out oError:string):Boolean;
  end;
  
implementation

{$IFDEF SERVER}
function TPmItemClassService.BeforeTreatment(const iItem:TPmItemClass; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  ItemDB:TPmItemClass;
  NewSortNo:Integer;
  NewId:string;
begin
  if iItem<>nil then
  begin
    ItemDB:=TPmItemClass.Create;
    try
      case iPreMode of
      pmAdd:
        begin
          if not CalculateIdAndSortNo(iItem.ItemClassTreePid,NewSortNo,NewId,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
          iItem.ItemClassId:=iItem.ObjGUID;
          iItem.ItemClassTreeId:=NewId;
          iItem.ItemClassIsSystem:=False;
          iItem.ItemClassStatus:=1;
          iItem.ItemClassSortNo:=NewSortNo;
          iItem.RecordDate:=Now;

        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItem.ClassName)) then
          begin
            if ItemDB.LoadFromStr(iItem.SaveToStr,oError) and
              Reload(ItemDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItem.ClassName),ItemDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItem.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;

          iItem.ModifyDate:=Now;
        end;
      pmDelete:
        begin
          if not Reload(iItem,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      end;
    finally
      FreeAndNil(ItemDB);
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeTreatment错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemClassService.BeforeTreatment(const iItems:TPmItemClassList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  Key:string;
  ItemsDB:TPmItemClassList;
begin
  if iItems<>nil then
  begin
    ItemsDB:=TPmItemClassList.Create;
    try
      case iPreMode of
      pmAdd:
        begin
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItems.ClassName)) then
          begin
            if ItemsDB.LoadFromStr(iItems.SaveToStr,oError) and
              Reload(ItemsDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItems.ClassName),ItemsDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemsDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItems.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
          if not Reload(iItems,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      end;
      for Key in iItems.SortKeys do
      begin
        if iPreMode=pmEdit then
        begin
          iParcel.PutStringGoods(AnsiString(iItems.Items[Key].ClassName),ItemsDB.Items[Key].SaveToStr);
        end;
        if not BeforeTreatment(iItems.Items[Key],iPreMode,iParcel,oError) then
        begin
          oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end;
    finally
      FreeAndNil(ItemsDB);
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeTreatment错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemClassService.BeforeCheck(const iItem:TPmItemClass; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemClassService.BeforeCheck(const iItems:TPmItemClassList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemClassService.Check(const iItem:TPmItemClass; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  ItemDB:TPmItemClass;
  SQL,SQLResultStr:string;
begin
  if iItem<>nil then
  begin
    ItemDB:=TPmItemClass.Create;
    try
      if SameStr(Trim(iItem.ItemClassId),EmptyStr) then
      begin
        oError:='物料大类ID不允许为空';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.ItemClassTreeId),EmptyStr) then
      begin
        oError:='物料大类树ID不允许为空';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.ItemClassTreePid),EmptyStr) then
      begin
        oError:='物料大类树PID不允许为空';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.ItemClassName),EmptyStr) then
      begin
        oError:='物料大类名称不允许为空';
        Result:=False;
        Exit;
      end else if iItem.ItemClassIsSystem then
      begin
        oError:='不允许创建、修改、删除系统级物料大类';
        Result:=False;
        Exit;
      end else if (iItem.ItemClassStatus<0) or (iItem.ItemClassStatus>1) then
      begin
        oError:='物料大类状态非法';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.OrgId),EmptyStr) then
      begin
        oError:='归属组织不允许为空';
        Result:=False;
        Exit;
      end;

      case iPreMode of
      pmAdd:
        begin
          if SameStr(Trim(iItem.RecordPsn),EmptyStr) then
          begin
            oError:='录入人不允许为空';
            Result:=False;
            Exit;
          end;
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItem.ClassName)) then
          begin
            if ItemDB.LoadFromStr(iItem.SaveToStr,oError) and
              Reload(ItemDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItem.ClassName),ItemDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItem.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
          if SameStr(Trim(iItem.ModifyPsn),EmptyStr) then
          begin
            oError:='物料大类人不允许为空';
            Result:=False;
            Exit;
          end else if iItem.ItemClassId<>ItemDB.ItemClassId then
          begin
            oError:='物料大类ID不允许修改';
            Result:=False;
            Exit;
          end else if iItem.ItemClassTreeId<>ItemDB.ItemClassTreeId then
          begin
            oError:='物料大类树ID不允许修改';
            Result:=False;
            Exit;
          end else if iItem.ItemClassTreePid<>ItemDB.ItemClassTreePid then
          begin
            oError:='物料大类树PID不允许修改';
            Result:=False;
            Exit;
          end else if iItem.ItemClassIsSystem<>ItemDB.ItemClassIsSystem then
          begin
            oError:='物料大类类型不允许修改';
            Result:=False;
            Exit;
          end else if not SameStr(Trim(iItem.RecordPsn),Trim(ItemDB.RecordPsn)) then
          begin
            oError:='录入人不允许修改';
            Result:=False;
            Exit;
          end else if not SameStr(Trim(iItem.OrgId),Trim(ItemDB.OrgId)) then
          begin
            oError:='归属组织不允许修改';
            Result:=False;
            Exit;
          end else if not SameDateTime(iItem.RecordDate,ItemDB.RecordDate) then
          begin
            oError:='录入日期不允许修改';
            Result:=False;
            Exit;
          end else if iItem.TimeStamp<>ItemDB.TimeStamp then
          begin
            oError:='物料大类信息已被他人修改，请重新装载...';
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
          SQL:=Format(string('SELECT COUNT(%s) FROM %s WHERE %s=''%s'' '),
                    [TPmItemDataModel.cItemClassIdKey,TPmItemDataModel.cTblName,
                    TPmItemDataModel.cItemClassIdKey,iItem.ItemClassId]);
          if TSK0001Comm.ReadSimpleData(fDBConn,fDBID,SQL,SQLResultStr,oError) then
          begin
            if StrToInt(SQLResultStr)>0 then
            begin
              oError:=Format(string('调用%s.Check错误...错误内容:当前物料大类已绑定物料,请先解除物料关系...'),[Self.ClassName]);
              Result:=False;
              Exit;
            end;
          end else begin
            oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;

          if iItem.ItemClassStatus<>0 then
          begin
            oError:='只允许删除已冻结物料大类';
            Result:=False;
            Exit;
          end
        end;
      end;
    finally
      FreeAndNil(ItemDB);
    end;
  end else begin
    oError:=Format(string('调用%s.Check错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemClassService.Check(const iItems:TPmItemClassList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  Key:string;
  ItemsDB:TPmItemClassList;
begin
  if iItems<>nil then
  begin
    ItemsDB:=TPmItemClassList.Create;
    try
      case iPreMode of
      pmAdd:
        begin
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItems.ClassName)) then
          begin
            if ItemsDB.LoadFromStr(iItems.SaveToStr,oError) and
              Reload(ItemsDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItems.ClassName),ItemsDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemsDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItems.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
        end;
      end;
      for Key in iItems.SortKeys do
      begin
        if iPreMode=pmEdit then
        begin
          iParcel.PutStringGoods(AnsiString(iItems.Items[Key].ClassName),ItemsDB.Items[Key].SaveToStr);
        end;
        if not Check(iItems.Items[Key],iPreMode,iParcel,oError) then
        begin
          oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end;
    finally
      FreeAndNil(ItemsDB);
    end;
  end else begin
    oError:=Format(string('调用%s.Check错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemClassService.BeforeExecute(const iItem:TPmItemClass; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  if iItem<>nil then
  begin
    case iPreMode of
    pmAdd:
      begin
        iItem.TimeStamp:=TSK0001Comm.GetTimeStampMs;
      end;
    pmEdit:
      begin
        iItem.TimeStamp:=TSK0001Comm.GetTimeStampMs;
      end;
    pmDelete:
      begin
      end;
    pmUse:
      begin
      end;
    pmUnUse:
      begin
      end;
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeExecute错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemClassService.BeforeExecute(const iItems:TPmItemClassList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  Key:string;
  ItemsDB:TPmItemClassList;
begin
  if iItems<>nil then
  begin
    ItemsDB:=TPmItemClassList.Create;
    try
      case iPreMode of
      pmAdd:
        begin
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItems.ClassName)) then
          begin
            if ItemsDB.LoadFromStr(iItems.SaveToStr,oError) and
              Reload(ItemsDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItems.ClassName),ItemsDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeExecute错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemsDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItems.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeExecute错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
        end;
      end;
      for Key in iItems.SortKeys do
      begin
        if iPreMode=pmEdit then
        begin
          iParcel.PutStringGoods(AnsiString(iItems.Items[Key].ClassName),ItemsDB.Items[Key].SaveToStr);
        end;
        if not BeforeCheck(iItems.Items[Key],iPreMode,iParcel,oError) then
        begin
          oError:=Format(string('调用%s.BeforeExecute错误 --> %s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end;
    finally
      FreeAndNil(ItemsDB);
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeExecute错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemClassService.AfterTreatment(const iItem:TPmItemClass; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemClassService.AfterTreatment(const iItems:TPmItemClassList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TPmItemClassService.CalculateIdAndSortNo(const iPid:string; out oNewSortNo:Integer; out oNewId,oError:string):Boolean;
var
  SQL,MaxId,NumStr,MaxSortNoStr:string;
  NewNum:Integer;
begin
  if SameStr(Trim(iPid),EmptyStr) then
  begin
    oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败, 错误内容:传入的iPid为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;

  SQL:=Format(string('SELECT MAX(%s) AS %s FROM %s WHERE %s=''%s'';'),
              [TPmItemClassModel.cItemClassTreeIdKey,TPmItemClassModel.cItemClassTreeIdKey,
              TPmItemClassModel.cTblName,TPmItemClassModel.cItemClassTreePidKey,
              iPid]);
  if TSK0001Comm.ReadSimpleData(fDBConn,EmptyStr,SQL,MaxId,oError) then
  begin
    if SameStr(Trim(MaxId),EmptyStr) then
    begin
      oNewId :=iPid+'.001';
    end else begin
      NumStr:=string(TSK0001Comm.AfterStr(AnsiString(MaxId),AnsiString(iPid+'.')));
      if Length(NumStr)=3 then
      begin
        NewNum:=StrToInt(NumStr[1])*100+StrToInt(NumStr[2])*10+StrToInt(NumStr[3])+1;
        if NewNum<10 then
        begin
          oNewId :=iPid+'.00'+IntToStr(NewNum);
        end else if NewNum<100 then
        begin
          oNewId :=iPid+'.0'+IntToStr(NewNum);
        end else if NewNum<1000 then
        begin
          oNewId :=iPid+'.'+IntToStr(NewNum);
        end else begin
          oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败,错误内容:新Id=1000 越界溢出!'),[Self.ClassName]);
          Result:=False;
          Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败,错误内容:截取的id尾数字符串长度不等于3!'),[Self.ClassName]);
        Result:=False;
        Exit;
      end;
    end;
  end else begin
    oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败,错误内容:获取最大同级Id失败!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;

  SQL:=Format(string('SELECT MAX(%s) AS %s FROM %s WHERE %s=''%s'';'),
              [TPmItemClassModel.cItemClassSortNoKey,TPmItemClassModel.cItemClassSortNoKey,
              TPmItemClassModel.cTblName,TPmItemClassModel.cItemClassTreePidKey,
              iPid]);
  if TSK0001Comm.ReadSimpleData(fDBConn,EmptyStr,SQL,MaxSortNoStr,oError) then
  begin
    if SameStr(Trim(MaxSortNoStr),EmptyStr) then
    begin
      oNewSortNo:=1;
    end else begin
      oNewSortNo:=StrToInt(MaxSortNoStr)+1;
    end;
  end else begin
    oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败, 错误内容:获取最大同级SortNo失败!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;
{$ENDIF}

function TPmItemClassService.LoadAll(const iItems:TPmItemClassList; out oError:string; const iJustUse:Boolean=False):Boolean;
var
  Condition,OrderBy:string;
begin
  if (iItems<>nil) then
  begin
    if iJustUse then
    begin
      Condition:=Format(string('%s=1'),[TPmItemClassModel.cItemClassStatusKey]);
    end else begin
      Condition:=EmptyStr;
    end;
    OrderBy:=TPmItemClassModel.cItemClassTreePidKey+','+TPmItemClassModel.cItemClassSortNoKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用TPmItemClassService.LoadAll方法失败！错误内容:传入的iItems为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemClassService.LoadById(const iItemClassId:string; const iItem:TPmItemClass; out oError:string):Boolean;
var
  Condition:string;
begin
  if not SameStr(iItemClassId,EmptyStr) and (iItem<>nil) then
  begin
    Condition:=Format(string('%s=''%s'''),[TPmItemClassModel.cItemClassIdKey,iItemClassId]);

    if not LoadByCondition(iItem,Condition,oError) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用TPmItemClassService.LoadById方法失败！错误内容:传入的iItemClassId或iItem为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemClassService.ExchangeSortNo(const iItem1:TPmItemClass; const iItem2:TPmItemClass; out oError:string):Boolean;
var
  TempSortNo:Integer;
  List:TPmItemClassList;
  NewItem1,NewItem2:TPmItemClass;
begin
  if (iItem1<>nil) and
    (iItem2<>nil) then
  begin
    List:=TPmItemClassList.Create;
    NewItem1:=TPmItemClass.Create;
    NewItem2:=TPmItemClass.Create;
    try
      if NewItem1.LoadFromStr(iItem1.SaveToStr,oError) and
        NewItem2.LoadFromStr(iItem2.SaveToStr,oError) then
      begin
        TempSortNo:=iItem1.ItemClassSortNo;
        NewItem1.ItemClassSortNo:=iItem2.ItemClassSortNo;
        NewItem2.ItemClassSortNo:=TempSortNo;

        List.Add(NewItem1);
        List.Add(NewItem2);
      end else begin
        oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容%s:'),[Self.ClassName,oError]);
        Result:=False;
        Exit;
      end;
      if Inherited Edit(List,oError) then
      begin
        if iItem1.LoadFromStr(NewItem1.SaveToStr,oError) and
          iItem2.LoadFromStr(NewItem2.SaveToStr,oError) then
        begin

        end else begin
          oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容%s:'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容%s:'),[Self.ClassName,oError]);
        Result:=False;
        Exit;
      end;
    finally
      List.Free;
    end;
  end else begin
    oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容:传入的iItem1或者iItem2为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemClassService.Use(const iItem:TPmItemClass; out oError:string):Boolean;
var
  Condition,SQL,SimpleResult,Key:string;
  List:TPmItemClassList;
begin
  if (iItem<>nil) then
  begin
    if iItem.ItemClassStatus=1 then
    begin
      Condition:=Format(string('%s LIKE ''%s'' '),[TPmItemClassModel.cItemClassTreeIdKey,iItem.ItemClassTreeId+'%']);
      List:=TPmItemClassList.Create;
      try
        if not LoadByCondition(List,Condition,oError) then
        begin
          oError:=Format(string('调用%s.Use方法失败！错误内容:%s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
        for Key in List.SortKeys do
        begin
          List.Items[Key].ItemClassStatus:=0;
          List.Items[Key].ModifyPsn:=iItem.ModifyPsn;
        end;
        if not inherited Edit(List,oError) then
        begin
          oError:=Format(string('调用%s.Use方法失败！错误内容:%s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      finally
        List.Free;
      end;
    end else begin
      SQL:=Format(string('SELECT %s FROM %s WHERE %s=''%s'' AND %s=%d'),
                  [TPmItemClassModel.cItemClassIdKey,TPmItemClassModel.cTblName,
                  TPmItemClassModel.cItemClassTreeIdKey,iItem.ItemClassTreePid,
                  TPmItemClassModel.cItemClassStatusKey,1]);
      if TSK0001Comm.ReadSimpleData(fDBConn,fDBID,SQL,SimpleResult,oError) then
      begin
        if SameStr(Trim(SimpleResult),EmptyStr) then
        begin
          oError:=Format(string('上级物料大类已停用,请先启用上级物料大类'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end else begin
          iItem.ItemClassStatus:=1;
          if not inherited Edit(iItem,oError) then
          begin
            oError:=Format(string('调用%s.Use错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      end else begin
        oError:=Format(string('调用%s.Use错误 --> %s'),[Self.ClassName,oError]);
        Result:=False;
        Exit;
      end;
    end;
  end else begin
    oError:=Format(string('调用%s.Use方法失败！错误内容:传入的iItem为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;
  
end.
